/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

static const void *HttpRequestHUDKey = &HttpRequestHUDKey;

@implementation UIViewController (HUD)

- (MBProgressHUD *)HUD{
    return objc_getAssociatedObject(self, HttpRequestHUDKey);
}

- (void)setHUD:(MBProgressHUD *)HUD{
    objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)showHudInView:(UIView *)view hint:(NSString *)hint{
    
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MBProgressHUD *HUD;
        
        if(![weakSelf HUD]){
            HUD = [[MBProgressHUD alloc] initWithView:view];
        }else{
            HUD = [weakSelf HUD];
        }
        
        HUD.label.text = hint;
        [view addSubview:HUD];
        [HUD showAnimated:YES];
        [weakSelf setHUD:HUD];

    
    });
    
  }

- (void)showHint:(NSString *)hint
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        //显示提示信息
        UIView *view = [[UIApplication sharedApplication].delegate window];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.userInteractionEnabled = NO;
        // Configure for text only and offset down
        hud.mode = MBProgressHUDModeText;
        hud.label.text = hint;
        hud.margin = 10.f;
        hud.offset = CGPointMake(hud.offset.x, IS_IPHONE_5?200.f:150.f);
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2];
        
    });
 
}

- (void)showHint:(NSString *)hint yOffset:(float)yOffset {
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        //显示提示信息
        UIView *view = [[UIApplication sharedApplication].delegate window];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.userInteractionEnabled = NO;
        // Configure for text only and offset down
        hud.mode = MBProgressHUDModeText;
        hud.label.text = hint;
        hud.margin = 10.f;
        hud.offset = CGPointMake(hud.offset.x, yOffset);
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2];
        
        
    });
    
}

- (void)hideHud{
    
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        
        MBProgressHUD *hud =[weakSelf HUD];
        [hud hideAnimated:YES];
        
        
    });
}

@end
